<?php

namespace app\api\controller;

use app\model\Task as TaskModel;
use app\model\Welfare;
use app\model\Staff;
use app\model\WorkTag;
use app\model\Collect;
use app\model\OnJob;
use think\facade\Env;
class Task extends Auth
{
    /**
     * 任务列表
     *
     * @return void
     */
    public function list()
    {
        $where[] = ['delete_time', '=', null];
        $where[] = ['is_show', '=', 0];
        // 岗位名称
        $work_name = input('work_name');
        if (!empty($work_name)) {
            $where[] = ['work_name|company_name', 'like', "%{$work_name}%"];
        }

        $address_sheng = input('address_sheng');
        if (!empty($address_sheng)) {
            $where[] = ['address_sheng', '=', $address_sheng];
        }

        $address_shi = input('address_shi');
        if (!empty($address_shi)) {
            $where[] = ['address_shi', '=', $address_shi];
        }

        $page = input('page');
        if (empty($page)) {
            $page = 1;
        }
        $limit = input('limit');
        if (empty($limit)) {
            $limit = 10;
        }
        $page_count = ($page - 1) * $limit;

        $list = TaskModel::where($where)
            ->field('id, work_name, company_name, welfare, pay_min, pay_max, recruit_num, address_sheng, address_shi, address_details, create_time')
            ->limit($page_count, $limit)
            ->withAttr('create_time', function($value, $data) {
                return date('m-d', strtotime($value));
            })
            ->order('create_time desc')
            ->select();
        foreach($list as $k => $v) {
            if (!empty($v['welfare'])) {
                $welfare_id = is_array($v['welfare']) ? $v['welfare']: json_decode($v['welfare'], true);
                $list[$k]['welfare_name'] = Welfare::whereIn('id', $welfare_id)->column('welfare_name');
            } else {
                $list[$k]['welfare_name'] = [];
            }
        }
        $count = TaskModel::where($where)
            ->count();
        success('任务列表', ['data' => $list, 'count' => $count, 'page' => $page]);
    }

    /**
     * 查看详情
     *
     * @return void
     */
    public function view()
    {
        $id = input('id');
        if (empty($id)) {
            error('缺少参数');
        }
        $info = TaskModel::where('id', $id)
            ->where('delete_time', null)
            ->find();
        if (!empty($info['welfare'])) {
            $welfare_id = is_array($info['welfare']) ? $info['welfare']: json_decode($info['welfare'], true);
            $info['welfare_name'] = Welfare::whereIn('id', $welfare_id)->column('welfare_name');
        } else {
            $info['welfare_name'] = [];
        }
        $is_collect = Collect::where('user_id', $this->getUserId())->where('task_id', $id)->find();
        if (!empty($is_collect)) {
            $info['is_collect'] = 1;
        } else {
            $info['is_collect'] = 0;
        }
        success('任务详情', $info);
    }

    /**
     * 收藏
     *
     * @return void
     */
    public function collect()
    {
        $task_id = input('id');
        if (empty($task_id)) {
            error('缺少参数');
        }
        $res = Collect::collectTask($this->getUserId(), $task_id);
        success($res);
    }

    /**
     * 人才列表
     *
     * @return void
     */
    public function staffList()
    {
        $where[] = ['boss_id', '=', $this->getUserId()];
        $where[] = ['delete_time', '=', null];
        $name = input('name');
        if (!empty($name)) {
            $where[] = ['name', 'like', "%{$name}%"];
        }
        $sex = input('sex');
        if (!empty($sex)) {
            $where[] = ['sex', '=', $sex];
        }

        $min_age = input('min_age');
        $max_age = input('max_age');
        if (!empty($min_age) && empty($max_age)) {
            $where[] = ['age', '>=', $min_age];
        }
        if (!empty($min_age) && !empty($max_age)) {
            $where[] = ['age', 'between', "{$min_age}, {$max_age}"];
        }

        $list = Staff::field('id, name, expect_work, sex, age')
            ->where($where)
            ->withAttr('expect_work', function($value, $data) {
                return json_decode($value);
            })
            ->select()->toArray();
        $work_tag = input('work_tag');
        if (!empty($work_tag)) {
            foreach ($list as $k => $v) {
                if (!array_intersect($v['expect_work'], $work_tag)) {
                    unset($list[$k]);
                }
            }
        }
        foreach ($list as $k => $v) {
            $list[$k]['work_name'] = WorkTag::where('id', 'in', $v['expect_work'])->column('tag');
        }

        success('人才列表', $list);
    }

    /**
     * 输送人才
     *
     * @return void
     */
    public function sendStaff()
    {
        $task_id = input('task_id');
        $staff_id = input('staff_id');
        if (empty($task_id) || empty($staff_id)) {
            error('缺少参数');
        }
        if (!is_array($staff_id)) {
            error('参数错误');
        }
        $res = OnJob::sendStaff($this->getUserId(), $task_id, $staff_id);
        if ($res === true) {
            success('操作成功');
        } else {
            error('操作失败');
        }
    }
}